using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using SJVars;
using SJXmlP.XMLClasses;
using System;
using System.Collections;
using System.Reflection;
using System.Text;

namespace yW5Lib
{
	public class clsParts : clsGenericBase<clsPart>
	{
		private clsXml cXml;

		public clsParts()
		{
			cXml = new clsXml();
		}

		public clsPart PartByID(int ID)
		{
			return base.get_ByID(ID);
		}

		public clsPart PartByIndex(int Index)
		{
			return base.get_ByIndex(Index);
		}

		public bool AddPart(ref clsPart Part)
		{
			if (Added(ref Part))
			{
				Part.AddedToProject = true;
				return true;
			}
			return false;
		}

		public void RemovePartByID(int ID)
		{
			if (IDUsed(ID))
			{
				RemoveByID(ID);
			}
			else
			{
				clsShared.cLog.AddAction("Tried to remove Part ID " + Conversions.ToString(ID) + " but it's not in the project.");
			}
		}

		public void RemovePartByIndex(int Index)
		{
			RemoveByIndex(Index);
		}

		public bool CreateFromXML(string XMLData)
		{
			bool result = true;
			try
			{
				Clear();
				string[] DataArray = new string[2];
				cXml.ExtractXMLArray(XMLData, "<PROJECTPARTS>", "</PROJECTPARTS>", "</PROJECTPART>", ref DataArray);
				int num = DataArray.Length - 1;
				for (int i = 0; i <= num; i++)
				{
					if (Strings.InStr(1, DataArray[i], "<PROJECTPART>", CompareMethod.Text) > 0)
					{
						clsPart Part = new clsPart();
						Part.CreateFromXML(DataArray[i]);
						if (Part.SortOrder == 0)
						{
							Part.SortOrder = i + 1;
						}
						if (!AddPart(ref Part))
						{
							result = false;
							clsShared.cLog.AddAction("Unable to add Part " + Part.Title + " to project.");
							return result;
						}
					}
				}
				DataArray = null;
				return result;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				result = false;
				ProjectData.ClearProjectError();
				return result;
			}
		}

		public bool WriteXML(ref StringBuilder SBP)
		{
			bool result = true;
			try
			{
				SBP.AppendLine("\t<PROJECTPARTS>");
				IEnumerator enumerator = default(IEnumerator);
				try
				{
					enumerator = base.colObjects.GetEnumerator();
					while (enumerator.MoveNext())
					{
						clsPart clsPart = (clsPart)enumerator.Current;
						SBP.AppendLine(clsPart.DataAsXML());
					}
				}
				finally
				{
					if (enumerator is IDisposable)
					{
						(enumerator as IDisposable).Dispose();
					}
				}
				SBP.AppendLine("\t</PROJECTPARTS>");
				return result;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				result = false;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
				return result;
			}
		}

		public void Resort()
		{
			bool flag;
			do
			{
				flag = true;
				int num = base.Count - 1;
				for (int i = 1; i <= num; i++)
				{
					clsPart clsPart = base.get_ByIndex(i);
					clsPart cObject = base.get_ByIndex(i + 1);
					if (cObject.SortOrder < clsPart.SortOrder)
					{
						flag = false;
						RemoveByID(cObject.ID);
						if (!Inserted(ref cObject, i))
						{
							AddPart(ref cObject);
							clsShared.cLog.AddAction("Error while sorting Parts - could not put " + cObject.Title + " back.");
							return;
						}
					}
				}
			}
			while (!flag);
		}
	}
}
